/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.view;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author SVN - Team
 */
public class RateItemComponent extends CustomItem {

    public static final int NONE = 0;
    public static final int EDIT_ABLE = 1;
    public static final int NO_EDIT_ABLE = 2;
    private Image rate[];
    private int ratePoint = 1;
    private int type;
    private RateItemComponent rateOverall;
    private RateItemComponent ratingOfServices;
    private RateItemComponent ratingOfAtmosphere;
    private RateItemComponent ratingOfFood;
    private RateItemComponent CURREN_STAGE;

    public void setRateOverall(RateItemComponent rateOverall) {
        this.rateOverall = rateOverall;
    }

    public void setRatingOfAtmosphere(RateItemComponent ratingOfAtmosphere) {
        this.ratingOfAtmosphere = ratingOfAtmosphere;
    }

    public void setRatingOfFood(RateItemComponent ratingOfFood) {
        this.ratingOfFood = ratingOfFood;
    }

    public void setRatingOfServices(RateItemComponent ratingOfServices) {
        this.ratingOfServices = ratingOfServices;
    }

    public RateItemComponent(String label, int type) {
        super(label);
        this.type = type;
        init();
    }

    public int getRatePoint() {
        return ratePoint;
    }

    public void setRatePoint(int ratePoint) {
        this.ratePoint = ratePoint;
        repaint();
    }

    protected int getMinContentHeight() {
        return 16;
    }

    protected int getMinContentWidth() {
        return 200;
    }

    protected int getPrefContentHeight(int width) {
        return getMinContentHeight();
    }

    protected int getPrefContentWidth(int height) {
        return getMinContentWidth();
    }

    private void init() {
        rate = new Image[6];
        try {
            rate[0] = Image.createImage("/rate_zero.png");
            rate[1] = Image.createImage("/rate_one.png");
            rate[2] = Image.createImage("/rate_two.png");
            rate[3] = Image.createImage("/rate_three.png");
            rate[4] = Image.createImage("/rate_four.png");
            rate[5] = Image.createImage("/rate_five.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    protected void paint(Graphics g, int w, int h) {
        g.drawString("Poor", 0, 2, Graphics.TOP | Graphics.LEFT);
        switch (ratePoint) {
            case 0:
                g.drawImage(rate[0], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            case 1:
                g.drawImage(rate[1], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            case 2:
                g.drawImage(rate[2], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            case 3:
                g.drawImage(rate[3], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            case 4:
                g.drawImage(rate[4], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            case 5:
                g.drawImage(rate[5], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
            default:
                g.drawImage(rate[0], (w / 2) - 15, h / 2, Graphics.VCENTER | Graphics.HCENTER);
                break;
        }
        g.drawString("Best", 140, 2, Graphics.TOP | Graphics.LEFT);
    }

    protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
        if (type != NO_EDIT_ABLE) {
            if(CURREN_STAGE != this) {
                CURREN_STAGE = this;
                return true;
            } else {
                switch (dir) {
                    case Canvas.LEFT:
                        ratePoint--;
                        if (ratePoint <= 1) {
                            ratePoint = 1;
                        }
                        repaint();
                        this.notifyStateChanged();
                        setOverallRating();
                        return true;
                    case Canvas.RIGHT:
                        ratePoint++;
                        if (ratePoint >= 5) {
                            ratePoint = 5;
                        }
                        repaint();
                        this.notifyStateChanged();
                        setOverallRating();
                        return true;
                    case NONE:
                        this.notifyStateChanged();
                        return true;
                }
                return false;
            }
        } else {
            return super.traverse(dir, viewportWidth, viewportHeight, visRect_inout);
        }
    }

    protected void traverseOut() {
        CURREN_STAGE = null;
        super.traverseOut();
    }

    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);
        if (type != NO_EDIT_ABLE) {
            if (x >= 30 && x <= 50) {
                ratePoint = 1;
            } else if (x > 50 && x <= 70) {
                ratePoint = 2;
            } else if (x > 70 && x <= 90) {
                ratePoint = 3;
            } else if (x > 90 && x <= 110) {
                ratePoint = 4;
            } else if (x > 110 && x <= 130) {
                ratePoint = 5;
            }
            repaint();
            setOverallRating();
        }
    }

    public void setOverallRating() {
        int totalRating = ratingOfAtmosphere.getRatePoint() + ratingOfFood.getRatePoint() + ratingOfServices.getRatePoint();
        int pointOverall = (int) totalRating / 3;
        rateOverall.setRatePoint(pointOverall);
    }
}
